// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

// inputs:	dataset_sitc.dta
//			dataset_hs8.dta
//			dataset_tsusa.dta

// outputs:	ecm_robustness1.tex
//			ecm_robustness1_lr.tex
//			ecm_robustness2.tex
//			ecm_robustness2_lr.tex
//			nntr_PS.tex
//			nntr_robustness1.tex
//			nntr_robustness2.tex
//			annualhs8_robustness.tex
//			annualtsusa_robustness.tex

// This code creates appendix tables G3--G9. Table G1 is created in 
// "12 Code final outputs/10 tables.ipynb". Table G2 is constructed in main.tex 
// as it is based on Xie et al (2020).

set varabbrev off

global dir_int "../20 Intermediate files/"
global dir_out "../31 Final output files appendix/"

loc g3 1
loc g4 1
loc g5 1
loc g6 1
loc g7 1
loc g8 1
loc g9 1

////////////////////////////////////////////////////////////////////////////////
// Table G3
if `g3' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009
	
	loc fe="sitcn#year sitcn#ctyn ctyn#year"
	loc treated chn
	loc cluster="cluster sitcn#ctyn"
	gen temp= phase==0 & fta==0 & (`treated'==1 | ntr==1)
	xtset id year
	eststo clear
	
	eststo : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1, absorb(`fe') vce(`cluster')
	nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro: -_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	eststo lr0
		
	eststo : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1, absorb(`fe') vce(`cluster')
	nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	eststo lr1
	
	loc controls="lshipping_jst"
	eststo : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if fta==0 & (`treated'==1 | ntr==1), absorb(`fe') vce(`cluster')
	nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	eststo lr2
	loc controls=""
	
	eststo : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if phase==0, absorb(`fe') vce(`cluster')
	nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	eststo lr3
	
	
	eststo : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls', absorb(`fe') vce(`cluster')
	nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	eststo lr4
	
	
	eststo : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1 & balanced_chn==1, absorb(`fe') vce(`cluster')
	nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	eststo lr5
	
	drop temp*
	esttab est1 est3 est5 est7 est9 est11, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum nocon stats(N r2_a,fmt("%9.0fc" "%3.2f"))
	esttab est1 est3 est5 est7 est9 est11 using "${dir_int}ecm_robustness1.tex", replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum booktabs nocon stats(N r2_a,fmt("%9.0fc" "%3.2f"))
	esttab lr0 lr1 lr2 lr3 lr4 lr5 using "${dir_int}ecm_robustness1_lr.tex", replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum booktabs nocon
}

////////////////////////////////////////////////////////////////////////////////
// Table G4
if `g4' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009
	
	loc fe="sitcn#year sitcn#ctyn ctyn#year"
	loc x lgap_med
	loc treated chn
	loc controls="lshipping_jst"
	loc cluster="cluster sitcn#ctyn"
	gen temp= phase==0 & fta==0 & (`treated'==1 | ntr==1)
	xtset id year
	eststo clear
	
	// STIC 1974-2008
	eststo sitcall: qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1, absorb(`fe') vce(`cluster')
	eststo sitcalllr: nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	
	// SITC 1974-88
	eststo sitcearly : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1 & year<1989, absorb(`fe') vce(`cluster')
	eststo sitcearlylr: nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	
	// SITC 1989-08
	eststo sitclate: qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1 & year>=1989, absorb(`fe') vce(`cluster')
	eststo sitclatelr: nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	
	// TSUSA 1974-88
	preserve
		use "${dir_int}dataset_tsusa.dta", clear
		loc fe="tsusan#year tsusan#ctyn ctyn#year"
		loc cluster="cluster tsusan#ctyn"
		xtset id year
		gen temp= fta==0 & (`treated'==1 | ntr_cty==1)
		eststo tsusa: qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1, absorb(`fe') vce(`cluster')
		eststo tsusalr: nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	restore
	
	// HS8 1989-08
	preserve
		use "${dir_int}dataset_hs8.dta", clear
		loc fe="hs8n#year hs8n#cty cty#year"
		loc cluster="cluster hs8n#cty"
		xtset id year
		gen temp= fta==0 & (`treated'==1 | ntr_cty==1) & phase==0
		eststo hs8: qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp==1 & year>=1989 & year<2009, absorb(`fe') vce(`cluster')
		eststo hs8lr: nlcom (lrc:-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst])) (lro:-_b[i0.`treated'#c.L.ltariff_jst]/(_b[i0.`treated'#c.L.lv_jst])), post
	restore
	
	drop temp*
	esttab sitcall tsusa sitcearly hs8 sitclate, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum nocon 
	esttab sitcalllr tsusalr sitcearlylr hs8lr sitclatelr, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum
	
	esttab sitcall tsusa sitcearly hs8 sitclate using "${dir_int}ecm_robustness2.tex", ///
	replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum stats(N r2_a,fmt("%9.0fc" "%3.2f")) booktabs nocon 
	
	esttab sitcalllr tsusalr sitcearlylr hs8lr sitclatelr  using "${dir_int}ecm_robustness2_lr.tex", ///
	replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum stats(N r2_a,fmt("%9.0fc" "%3.2f")) booktabs nocon 
}

////////////////////////////////////////////////////////////////////////////////
// Table G5
if `g5' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	loc FE="absorb(ctyn#year sitcn#ctyn sitcn#year)"
	loc x lgap_med
	loc treated chn
	loc cluster="cluster sitcn#ctyn"
	gen temp= 1
	eststo clear
	loc controls="ltariff_jst"
	eststo : qui reghdfe lv_jst i0.postWTO#i1.`treated'#c.`x' `controls' if year>1991 & year<2008 & temp==1, `FE' vce(`cluster')
	eststo : qui reghdfe lv_jst i0.postWTO#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	loc x lnntr_med
	eststo : qui reghdfe lv_jst i0.postWTO#i1.`treated'#c.`x' `controls' if year>1991 & year<2008 & temp==1, `FE' vce(`cluster')
	eststo : qui reghdfe lv_jst i0.postWTO#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	loc x lapplied_nntr
	eststo : qui reghdfe lv_jst i0.postWTO#i1.`treated'#c.`x' `controls' if year>1991 & year<2008 & temp==1, `FE' vce(`cluster')
	eststo : qui reghdfe lv_jst i0.postWTO#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	drop temp*
	
	esttab, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum nocon
	esttab using "${dir_int}nntr_PS.tex", replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum booktabs stats(N r2_a,fmt("%9.0fc" "%3.2f")) nocon
}

////////////////////////////////////////////////////////////////////////////////
// Table G6
if `g6' == 1{
	use "${dir_int}dataset_tsusa.dta", clear

	loc FE="absorb(tsusan#year tsusan#ctyn ctyn#year)"
	g tempx =  lnntr_chn
	loc treated chn
	loc controls=""
	loc cluster="cluster tsusan#ctyn"
	gen temp= fta==0 & (`treated'==1 | ntr_cty==1)
	eststo clear
	
	// Baseline
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1, `FE' vce(`cluster')
	// All countries
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls', `FE' vce(`cluster')
	drop tempx
	
	// Avg NNTR
	g tempx = lnntr_allcom
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1, `FE' vce(`cluster')
	drop tempx
	
	// Tariffs
	g tempx = lnntr_chn
	loc controls="ltariff_jst"
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1, `FE' vce(`cluster')
	drop temp*
	
	esttab, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum nocon
	esttab using "${dir_int}annualtsusa_robustness.tex", replace b(2) se(2) ar2(2)star(* 0.10 ** 0.05 *** 0.01) /// 
	label nonum booktabs nocon stats(N r2_a,fmt("%9.0fc" "%3.2f"))
}

////////////////////////////////////////////////////////////////////////////////
// Table G7
if `g7' == 1{
	use "${dir_int}dataset_hs8.dta", clear

	drop if year>2008 
	loc FE="absorb(hs8n#year hs8n#cty cty#year)"
	loc x lgap_2001
	loc treated chn
	loc controls="ltariff_jst"
	loc cluster="cluster hs8n#cty"
	gen temp= fta==0 & (`treated'==1 | ntr_cty==1) & phase==0
	eststo clear
	
	// Baseline
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	
	//No tariffs
	loc controls=""
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	
	// All countries
	loc controls="ltariff_jst"
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if phase==0, `FE' vce(`cluster')
	
	// Full
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls', `FE' vce(`cluster')

	// Balanced
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if temp==1 & balanced_chn==1, `FE' vce(`cluster')
	drop temp*
	
	esttab, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum nocon
	esttab using "${dir_int}annualhs8_robustness.tex", replace b(2) se(2) ar2(2)star(* 0.10 ** 0.05 *** 0.01) /// 
	label nonum booktabs nocon stats(N r2_a,fmt("%9.0fc" "%3.2f"))
}

////////////////////////////////////////////////////////////////////////////////
// Table G8
if `g8' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	loc FE="absorb(sitcn#year sitcn#ctyn ctyn#year)"
	g tempx = lgap_med 
	loc treated chn
	loc lastyr 2008
	loc firstyr 1974
	loc controls=""
	loc cluster="cluster sitcn#ctyn"
	gen temp= phase==0 & fta==0 & (`treated'==1 | ntr==1)
	eststo clear
	*baseline
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1 & year<2009, `FE' vce(`cluster')
	*All countries
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if phase==0 & year<2009, `FE' vce(`cluster')
	*Full sample
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if year<2009, `FE' vce(`cluster')
	*Balanced
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1 & balanced_chn==1 & year<2009, `FE' vce(`cluster')
	*** Only U.S. imports
	loc FE="absorb(sitcn#ctyn ctyn#year)"
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if phase==0 & year<2009 & chn==1, `FE' vce(`cluster')
	loc FE="absorb(sitcn#year sitcn#ctyn ctyn#year)"
	drop temp*
	esttab, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum nocon
	esttab using "${dir_int}nntr_robustness1.tex", replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum booktabs stats(N r2_a,fmt("%9.0fc" "%3.2f")) nocon
}

////////////////////////////////////////////////////////////////////////////////
// Table G9
if `g9' == 1{
	use "${dir_int}dataset_sitc.dta", clear

	bysort sitc year: egen temp_mfn = mean(tariff_jst/(ntr==1))
	g temp_gap = nntr_med - temp_mfn 
	replace temp_gap=0 if temp_gap<0 & temp_gap!=.
	g temp_gap_time = log(1+temp_gap)

	loc FE="absorb(sitcn#year sitcn#ctyn ctyn#year)"
	loc treated chn
	loc lastyr 2008
	loc firstyr 1974
	loc controls=""
	loc cluster="cluster sitcn#ctyn"
	gen temp= phase==0 & fta==0 & (`treated'==1 | ntr==1)
	xtset id year
	eststo clear
	*NNTR rate
	g tempx = lnntr_med
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1 & year<2009, `FE' vce(`cluster')
	*Applied NNTR rate
	drop tempx
	g tempx = lapplied_nntr
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1 & year<2009, `FE' vce(`cluster')
	*Time Varying NTR Gap
	drop tempx
	g tempyear=year==2008
	g tempx = i0.tempyear#temp_gap_time
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx i1.tempyear#i1.`treated'#c.tempx `controls' if temp==1 & year<2009, `FE' vce(`cluster')
	*shipping
	drop tempx
	g tempx = lgap_med
	loc controls="lshipping_jst"
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1 & year<2009, `FE' vce(`cluster')
	*tariffs
	loc controls="ltariff_jst lshipping_jst"
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1 & year<2009, `FE' vce(`cluster')
	*anticipation
	loc controls="F1.S1.ltariff_jst"
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.tempx `controls' if temp==1 & year<2009, `FE' vce(`cluster')
	drop temp*
	esttab, replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum nocon
	esttab using "${dir_int}nntr_robustness2.tex", replace b(2) se(2) ar2(2) star(* 0.10 ** 0.05 *** 0.01) label nonum booktabs stats(N r2_a,fmt("%9.0fc" "%3.2f")) nocon	
}